package com.copa.l1807;

import java.util.*;

/**
 * @author copa
 * @createDate 2023-01-12 16:14
 * @function 哈希表+字符串替换[正则](超时)
 */
public class Test1 {

    public String evaluate(String s, List<List<String>> knowledge) {
        Map<String, String> keyContentMap = new HashMap<>();
        for (List<String> keyContent : knowledge) {
            keyContentMap.put(keyContent.get(0), keyContent.get(1));
        }
        while (s.contains("(")
                && s.contains(")")
                && s.indexOf("(") < s.indexOf(")")) {
            String key = s.substring(s.indexOf("(") + 1, s.indexOf(")"));
            if (Objects.nonNull(keyContentMap.get(key))) {
                s = s.replace("("+ key +")", keyContentMap.get(key));
                continue;
            }
            s = s.replace("("+ key +")", "?");
        }
        return s;
    }

    public static void main(String[] args) {
        String s = "(name)is(age)yearsold";
        List<List<String>> knowledge = new ArrayList<>();
        List<String> keyContent = new ArrayList<>();
        keyContent.add("name");
        keyContent.add("bob");
        knowledge.add(keyContent);
        keyContent = new ArrayList<>();
        keyContent.add("age");
        keyContent.add("two");
        knowledge.add(keyContent);
        System.out.println(new Test1().evaluate(s, knowledge));
    }
}
